﻿using Cloud.Test.Main.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cloud.Test.Main
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DirectoryInfo directInfo = null;
        string baseDirectory = null;
        string appName = null;
        string backUpPath = null;
        string downLoadPath = null;
        string unZipPath = null;
        public MainWindow()
        {
            InitializeComponent();
            baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            directInfo = new DirectoryInfo(baseDirectory);//获取文件
            appName = AppDomain.CurrentDomain.BaseDirectory + @"\Cloud.Test.Main.exe";//主程序路径
            backUpPath = directInfo.Parent.FullName + "/backup/";//程序备份路径(不需要备份就不填)
            downLoadPath = directInfo.Parent.FullName + "/download/temp/";//下载包临时存放位置 可默认
            unZipPath = AppDomain.CurrentDomain.BaseDirectory;//解压文件存放位置,可默认
            Init();


        }
        void Init() {
            var currentVersion = UpdateUtil.Read("versions", "current_version", null, directInfo.Parent.FullName + @"\version.ini");
            var appVersion = UpdateUtil.Read("versions", "app_version", null, directInfo.Parent.FullName + @"\version.ini");
            version.Text = "当前版本:" + currentVersion;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            ClientParameter clientParameter = new ClientParameter {
                AppName = appName,
                BackUpPath = backUpPath,
                DownLoadPath = downLoadPath,//下载包临时存放位置 可默认
                UnZipPath = unZipPath,//解压文件存放位置,可默认
                UpdateLogUrl = "",//更新成功后会自动跳转至该路径 为空则不跳转
                UpdateVersions = new List<UpdateVersion> {
                            new UpdateVersion("1.0.2","http://127.0.0.1:5000/v3.23.12_202312051713.zip"),
                            new UpdateVersion("1.0.3","http://127.0.0.1:5000/Avalonia-11.0.0-preview4.zip"),
                            //new UpdateVersion("1.0.4","http://127.0.0.1:5000/Dragablz-master.zip"),
                            new UpdateVersion("1.0.5","http://127.0.0.1:5000/EChartsSDK-master.zip"),
                            new UpdateVersion("1.0.6","http://127.0.0.1:5000/EChartsNet-master.zip"),
                            //new UpdateVersion("1.0.7","http://127.0.0.1:5000/HandyControl-master.zip"),
                            new UpdateVersion("1.0.8","http://127.0.0.1:5000/Live-Charts-master.zip"),
                        }
            };


            string arg = UpdateUtil.Serialize(clientParameter);

            var path = directInfo.Parent.FullName + @"\download\Cloud.Update.exe";
            if (File.Exists(path))
            {
                Process.Start(path, arg);
                Process.GetCurrentProcess().Kill();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DirectoryInfo directInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);//获取文件
            ClientParameter clientParameter = new ClientParameter
            {
                IsRestore = true,
                BackUpPath = directInfo.Parent.FullName + "/backup/",
                AppName = AppDomain.CurrentDomain.BaseDirectory + @"\Cloud.Test.Main.exe",
                UnZipPath = AppDomain.CurrentDomain.BaseDirectory,//解压文件存放位置,可默认

            };

            string arg = UpdateUtil.Serialize(clientParameter);

            var path = directInfo.Parent.FullName + @"\download\Cloud.Update.exe";
            if (File.Exists(path))
            {
                Process.Start(path, arg);
                Process.GetCurrentProcess().Kill();
            }
        }
    }
}
